package com.ewzj.bigdata.mvp.presenter;

import android.Manifest;
import android.app.Application;

import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.ewzj.bigdata.app.BdAppLocationListener;
import com.ewzj.bigdata.mvp.ui.activity.LogoActivity;
import com.jess.arms.integration.AppManager;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.http.imageloader.ImageLoader;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import javax.inject.Inject;

import com.ewzj.bigdata.mvp.contract.LogoContract;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.utils.PermissionUtil;

import java.util.List;


@ActivityScope
public class LogoPresenter extends BasePresenter<LogoContract.Model, LogoContract.View> {
    private RxErrorHandler mErrorHandler;
    private Application mApplication;
    private ImageLoader mImageLoader;
    private AppManager mAppManager;
    private LocationClient mLocationClient;
    private BdAppLocationListener bdLocationListener;
    @Inject
    public LogoPresenter (LogoContract.Model model, LogoContract.View rootView
            , RxErrorHandler handler, Application application
            , ImageLoader imageLoader, AppManager appManager) {
        super(model, rootView);
        this.mErrorHandler = handler;
        this.mApplication = application;
        this.mImageLoader = imageLoader;
        this.mAppManager = appManager;
    }

    /**
     * 6.0以上权限检测，地图定位需要
     */

    public void requestPermissons(){
        String [] permissions={Manifest.permission.READ_PHONE_STATE
                ,Manifest.permission.ACCESS_FINE_LOCATION
                ,Manifest.permission.ACCESS_COARSE_LOCATION
                ,Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE
        };
        PermissionUtil.requestPermission(new PermissionUtil.RequestPermission() {
            @Override
            public void onRequestPermissionSuccess() {
                mRootView.showAdvert();
                locationStart();
            }

            @Override
            public void onRequestPermissionFailure(List<String> permissions) {
                ArmsUtils.snackbarText("请先开启定位权限");
                mRootView.showAdvert();
            }

            @Override
            public void onRequestPermissionFailureWithAskNeverAgain(List<String> permissions) {
                mRootView.showAdvert();
            }
        },((LogoActivity)mRootView).getmRxPermissions(),mErrorHandler,permissions);
    }

    /**
     * 初始化百度地图
     */
    public void initBaiduconfig(){
        LocationClientOption option = new LocationClientOption();

        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
        option.setCoorType("bd09ll");
        //如果设置为0，则代表单次定位，即仅定位一次，默认为0,如果设置非0，需设置1000ms以上才有效
        option.setScanSpan(0);
        //可选，设置是否使用gps，默认false
        //使用高精度和仅用设备两种定位模式的，参数必须设置为true
        option.setOpenGps(true);
        option.setLocationNotify(true);
        //可选，设置是否当GPS有效时按照1S/1次频率输出GPS结果，默认false

        option.setIgnoreKillProcess(true);
        //可选，定位SDK内部是一个service，并放到了独立进程。
        //设置是否在stop的时候杀死这个进程，默认（建议）不杀死，即setIgnoreKillProcess(true)

        option.SetIgnoreCacheException(false);
        //可选，设置是否收集Crash信息，默认收集，即参数为false
        option.setIsNeedAddress(true);
        option.setWifiCacheTimeOut(5*60*1000);
        //可选，7.2版本新增能力
        //如果设置了该接口，首次启动定位时，会先判断当前WiFi是否超出有效期，若超出有效期，会先重新扫描WiFi，然后定位

        option.setEnableSimulateGps(false);
        //可选，设置是否需要过滤GPS仿真结果，默认需要，即参数为false
        mLocationClient=new LocationClient(mApplication);
        mLocationClient.setLocOption(option);
        //mLocationClient为第二步初始化过的LocationClient对象
        //需将配置好的LocationClientOption对象，通过setLocOption方法传递给LocationClient对象使用
        //更多LocationClientOption的配置，请参照类参考中LocationClientOption类的详细说明
        bdLocationListener=new BdAppLocationListener();
        mLocationClient.registerLocationListener(bdLocationListener);
    }

    /**
     *
     * 发起定位，获取位置信息
     */
    public void locationStart(){
        mLocationClient.start();
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mLocationClient.isStarted()){
            mLocationClient.stop();
        }
        this.mLocationClient=null;
        this.bdLocationListener=null;
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }

}
